#include <string.h>
#include "NonDeterminism.hpp"

using namespace BFT::messages;

NonDeterminism::NonDeterminism(uint64_t _time, uint64_t _seeds)
{
	time = _time;
	seeds = _seeds;
	time_offset = 1232655613/*952*/;
	bytes.reset( (char*)malloc(NON_DETERMINISM_SIZE), free);
	
	*(uint64_t*)(bytes.get()) = htonll(time);
	*(uint64_t*)(bytes.get() + NON_DETERMINISM_SIZE/2) = htonll(seeds);	
}

NonDeterminism::NonDeterminism(boost::shared_ptr<char> _bytes, int _bytes_size)
{
	if(_bytes_size != NON_DETERMINISM_SIZE)
	{
		fprintf(stderr,"trying to construct a NonDeterminism from invalid bytes\n");
		exit(152);
	}

	//TODO ignoring _bytes_syze. fixed size in the current implementation
	time_offset = 1232655613/*952*/;
	bytes = _bytes;
	time =  ntohll( *(uint64_t*)(bytes.get()) );
	seeds =  ntohll( *(uint64_t*)(bytes.get()+NON_DETERMINISM_SIZE/2) );

}

int NonDeterminism::size()
{
	return NON_DETERMINISM_SIZE;
}

bool NonDeterminism::equals(boost::shared_ptr<NonDeterminism> _nondet)
{
	return ( time == _nondet.get()->getTime() && seeds == _nondet.get()->getSeeds());
}
